package edu.neu.cs6515.xml.converter;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import edu.neu.cs6515.xml.IngeniousColor;
import edu.neu.cs6515.xml.Placement;

public class PlacementConverter implements Converter {

    @Override
    public boolean canConvert(final Class type) {
        return Placement.class.equals(type);
    }

    @Override
    public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
        Placement p = (Placement) source;
        writer.addAttribute("c0", p.getC0().toString());
        writer.addAttribute("c1", p.getC1().toString());
        writer.addAttribute("a0", String.valueOf(p.getA0()));
        writer.addAttribute("d0", String.valueOf(p.getD0()));
        writer.addAttribute("a1", String.valueOf(p.getA1()));
        writer.addAttribute("d1", String.valueOf(p.getD1()));
    }

    @Override
    public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
        IngeniousColor c0 = IngeniousColor.valueOf(reader.getAttribute("c0"));
        IngeniousColor c1 = IngeniousColor.valueOf(reader.getAttribute("c1"));
        int a0 = Integer.valueOf(reader.getAttribute("a0"));
        int a1 = Integer.valueOf(reader.getAttribute("a1"));
        int d0 = Integer.valueOf(reader.getAttribute("d0"));
        int d1 = Integer.valueOf(reader.getAttribute("d1"));
        return new Placement(c0, c1, d0, a0, d1, a1);
    }

}